package cqut.gbq.qimo.service.impl;

import cqut.gbq.qimo.entity.ResBean;
import cqut.gbq.qimo.entity.Teacher;
import cqut.gbq.qimo.mapper.TeacherMapper;
import cqut.gbq.qimo.service.TeacherService;
import cqut.gbq.qimo.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName TeacherServiceImpl
 * @Description TODO
 * @Author guobenqi
 * @Date 2021/6/17 14:22
 */
@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;
    @Override
    public Teacher login(String username, String password) {
        return teacherMapper.findOneBYUsernameAndPassword(username,password);
    }

    @Override
    public ResBean register(String username, String password, String school, String name) {
//        Teacher teacher =  teacherMapper.findOneBYUsernameAndPassword(username,password);
        Teacher teacher = teacherMapper.findOneByUserName(username);
        if (teacher != null){
            return ResBean.error("该老师已经注册");
        }else{
            Integer count = teacherMapper.insertTeacher(username,password,school,name);
            if (count > 0){
                return ResBean.success("注册成功");
            }else{
                return ResBean.error("注册失败");
            }

        }
    }

    @Override
    public Teacher getTeacherByTid(Integer tid) {
        return teacherMapper.getTeacherByTid(tid);
    }

    @Override
    public Integer getNotSubmit(Integer tid) {
        Integer number = teacherMapper.getNotSubmit(tid);
        if (number >= 0){
            return number;
        } else {
            return null;
        }
    }

    @Override
    public Integer getNotCheck(Integer tid) {
        Integer number = teacherMapper.getNotCheck(tid);
        if (number >= 0){
            return number;
        } else {
            return null;
        }
    }

    @Override
    public Integer getCheck(Integer tid) {
        Integer number = teacherMapper.getCheck(tid);
        if (number >= 0){
            return number;
        } else {
            return null;
        }
    }

    @Override
    public ResBean editTeacher(Teacher teacher) {
        Integer count = 0;
        if (teacher.getPassword() != null){
            String password = MD5Utils.MD5(teacher.getPassword());
           count =  teacherMapper.updatePassword(password,teacher.getTid());
           if (count > 0){
               return ResBean.success("修改教师基信息成功");
           }else{
               return ResBean.error("修改教师基信息失败");
           }
        }else{
            count = teacherMapper.updateTeacherInfo(teacher.getTid(),teacher.getName(),teacher.getSchool(),teacher.getTeacherWorkId());
            if (count > 0){
                return ResBean.success("修改教师密码成功");
            }else{
                return ResBean.error("修改教师密码失败");
            }
        }
    }
}
